<?php
/**
 * Exception events
 *
 * @package    event
 * @subpackage event
 * @author 	   Gerrit Jan van Ahee <gerrit.jan@yes2web.nl>
 * @copyright  2010-2011 Yes2web B.V.
 * @license    MIT License http://www.opensource.org/licenses/mit-license.php
 * @version    1.0.0
 * @link 	   www.yes2web.nl
 * @see        Event_Event
 */
/**
 * Event_Exceptions should be triggered by any Exception you throw
 *
 * The Event_Exception is a special type of event, holding some additional
 * information such as the Exception triggering the event. In ordinary Event_Events,
 * this object would be available through the getSource() method. Event_Exceptions
 * provide the instance throwing the Exception though the getSource() method. The
 * Exception itself is accessable through the getException() method.
 *
 * @package    event
 * @subpackage event
 * @author 	   Gerrit Jan van Ahee <gerrit.jan@yes2web.nl>
 * @copyright  2010-2011 Yes2web B.V.
 * @license    MIT License http://www.opensource.org/licenses/mit-license.php
 * @version    1.0.0
 * @link 	   www.yes2web.nl
 * @see        Event_Event
 */
class Event_Exception extends Event_Event
{
	/**
	 * @var Exception The exception thrown
	 */
	protected $oException;
	
	/**
	 * A new Event_Exception to signal handlers of a new Exception created
	 */	
	public function __construct()
	{
		$amTrace = debug_backtrace();
		// remove reference to this call
		array_shift($amTrace);	
		// trace from $oEx->getTrace() differs substantially from debug_backtrace() 
		$this->oException = $amTrace[0]['object'];
		
		if (false === ($this->oException instanceof Exception)) {
			throw new Exception('Event_Exception triggered by a non-Exception', 101);
		}
		
		parent::__construct(
			'exception',
			array(
				'message' => $this->oException->getMessage(),
				'code' => $this->oException->getCode()
			),
			$amTrace
		);
	}
	
	/**
	 * Get the Exception instance that triggered this Event. The object throwing /
	 * creating the Exception is available throw {@link getSource()}
	 *
	 * @return Exception The exception triggering this event
	 */
	public function getException()
	{
		return $this->oException;
	}
}